package com.wandian.ui.controls
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Point;




	public class TabNavigator extends UIComponent
	{
		public var tabContainer:Sprite
		public var contentContainer:Sprite
		private var tabQueue:Array=[];
		private var _tabHeight:int=20;
		private var _selectedTab:Tab;
		private var _contentPoint:Point;

		public function TabNavigator()
		{
			super();
		}

		public function set contentPonit(p:Point):void
		{
			if (p == null)
			{
				return
			}
			_contentPoint=p
			contentContainer.x=p.x
			contentContainer.y=p.y
		}

		public function get contentPonit():Point
		{
			return _contentPoint;
		}

		override protected function createChildren():void
		{
			tabContainer=new Sprite()
			this.addChild(tabContainer);
			contentContainer=new Sprite();
			this.addChild(contentContainer);
			contentPonit=new Point(0, 32);
		}

		public function addTab(label:String, content:TabContent):Tab
		{

			var tab:Tab=new Tab()
			var skinClass:Class=getStyle("tabSkin")
			tab.setStyle("skin", skinClass);
			tab.initialize()
			tab.label=label
			tab.content=content

			tabQueue.push(tab)
			tabContainer.addChild(tab);
			tab.addEventListener(MouseEvent.MOUSE_DOWN, onTabSelected)
			updateTabPosition();

			return tab;
		}

		private function onTabSelected(e:MouseEvent):void
		{
			selectedTab=e.target as Tab
		}

		private function updateTabPosition():void
		{
			var space:int=getStyle("tabSpace")
			var pos:int=0
			for (var i:int=0; i < tabQueue.length; i++)
			{
				var tab:Tab=tabQueue[i];
				tab.x=pos
				pos=pos + tab.width + space
			}
		}

		private function setTabSelected(tab:Tab, selected:Boolean):void
		{
			if (tab == null)
			{
				return;
			}
			tab.selected=selected
			var content:TabContent=tab.content
			if (content == null)
			{
				return;
			}
			if (selected)
			{
				contentContainer.addChild(content)
				content.onActive()
			}
			else
			{
				if (contentContainer.contains(content))
				{
					contentContainer.removeChild(content);
					content.onInactive();
				}
			}
		}

		public function get selectedTab():Tab
		{
			return _selectedTab;
		}

		public function set selectedTab(v:Tab):void
		{
			if (_selectedTab == v)
			{
				return;
			}
			setTabSelected(_selectedTab, false)
			_selectedTab=v;
			setTabSelected(_selectedTab, true)
		}
	}

}
